"""
Case Type   : 资源池化集群DMS reform优化
Case Name   : 踢出备1、备2->备1、备2同时加入
Create At   : 2024/05/13
Owner       : @kyleze
Description :
    1.踢出备机1,备2
    2.加入备机1、备2
    4.查看集群状态
Expect      :
    1.成功
    2.成功
    3.集群状态正常
History     :
"""

import os
import unittest
import time

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH

from yat.test import Node
from yat.test import macro


class DmsRform(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.dirname(__file__)} start----')
        self.com = Common()
        self.constant = Constant()
        self.primary_sh = CommonSH("PrimaryDbUser")
        self.pri_user_node = Node(node="PrimaryDbUser")

    def test_dms(self):
        step = '--step1:踢出备机1 expect:成功--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl("stop", '-n 2')
        self.log.info(result)
        self.assertIn(self.constant.cm_stop_node_success_msg, "执行失败" + step)
        result = self.primary_sh.exec_cm_ctl("stop", '-n 3')
        self.log.info(result)
        self.assertIn(self.constant.cm_stop_node_success_msg, "执行失败" + step)

        step = '--step2:加入备机1、备2 expect:成功--'
        self.log.info(step)
        result = self.pri_user_node.sh(
            f"source {macro.DB_ENV_PATH};"
            f"cm_ctl start -n 2 && cm_ctl start -n 3").result()
        self.log.info(result)
        self.assertEqual(
            2, result.count(self.constant.cm_start_node_success_msg),
            "执行失败" + step)

        step = '--step4:查看集群状态 expect:集群状态正常--'
        self.log.info(step)
        time.sleep(5)
        status = self.primary_sh.get_db_cluster_status('status')
        self.assertTrue("Normal" in status)

    def tearDown(self):
        self.log.info(f'----{os.path.dirname(__file__)} end----')

